#include "SettingsWidget.h"
#include "Settings.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>

SettingsWidget::SettingsWidget(QWidget *parent) :
	QDialog(parent)
{
	askBeforeExit = new QCheckBox("ask before exit", this);
	askBeforeExit->setChecked(Settings::getAskBeforeExit());

	askBeforeStopDownload = new QCheckBox("ask before stop download", this);
	askBeforeStopDownload->setChecked(Settings::getAskBeforeStopDownload());

	askBeforeRemoveDownload = new QCheckBox("ask before remove running download", this);
	askBeforeRemoveDownload->setChecked(Settings::getAskBeforeRemoveDownload());

	askBeforeOverwrite = new QCheckBox("ask before overwrite existing file", this);
	askBeforeOverwrite->setChecked(Settings::getAskBeforeOverwrite());

	maxParallelDownloadsNumber = new QSpinBox(this);
	maxParallelDownloadsNumber->setRange(1, 10);
	maxParallelDownloadsNumber->setValue(Settings::getMaxParallelDownloadsNumber());

	QLabel *label = new QLabel("maximal number of simultaneous downloads", this);
	QHBoxLayout *parallelDownloadsLayout = new QHBoxLayout;
	parallelDownloadsLayout->addWidget(maxParallelDownloadsNumber);
	parallelDownloadsLayout->addWidget(label);


	QPushButton *okButton = new QPushButton("Ok", this);
	connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));

	QPushButton *cancelButton = new QPushButton("Cancel", this);
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

	QHBoxLayout *buttonsLayout = new QHBoxLayout;
	buttonsLayout->addStretch();
	buttonsLayout->addWidget(okButton);
	buttonsLayout->addWidget(cancelButton);

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(askBeforeExit);
	mainLayout->addWidget(askBeforeStopDownload);
	mainLayout->addWidget(askBeforeRemoveDownload);
	mainLayout->addWidget(askBeforeOverwrite);
	mainLayout->addLayout(parallelDownloadsLayout);
	mainLayout->addLayout(buttonsLayout);

	setLayout(mainLayout);
}

void SettingsWidget::accept()
{
	Settings::saveAskBeforeExit(askBeforeExit->isChecked());
	Settings::saveAskBeforeStopDownload(askBeforeStopDownload->isChecked());
	Settings::saveAskBeforeRemoveDownload(askBeforeRemoveDownload->isChecked());
	Settings::saveAskBeforeOverwrite(askBeforeOverwrite->isChecked());
	Settings::saveMaxParallelDownloadsNumber(maxParallelDownloadsNumber->value());

	QDialog::accept();
}
